package org.jix.weblog.web.markdown.provider;

import org.commonmark.node.Link;
import org.commonmark.node.Node;
import org.commonmark.renderer.html.AttributeProvider;

import java.util.Map;

/**
 * @version 1.0
 * @Author Jix
 * @Date 2024/9/13 15:03
 * @注释 防链接追踪
 */


public class NofollowLinkAttributeProvider implements AttributeProvider {

    /**
     * 网站域名，上线后改成自己的即可
     */
    private final static String DOMAIN = "www.example.com";

    @Override
    public void setAttributes(Node node, String tagName, Map<String, String> attributes) {
        if (node instanceof Link) {
            Link linkNode = (Link) node;
            //获取链接地址
            String href = linkNode.getDestination();
            //若非自己的域名，则添加rel=“nofollow”属性
            if (!href.contains(DOMAIN)) {
                attributes.put("rel", "nofollow");
            }
        }
    }
}
